package com.yy.bootdemo.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.bootdemo.admin.entity.ScdQaArticleEntity;
import com.yy.bootdemo.admin.page.ArticlePage;
import com.yy.bootdemo.admin.service.ScdQaArticleService;
import com.yy.bootdemo.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;


/**
 * 常见问题回答
 *
 * @author yy
 * @email yangxxil01@sina.com
 * @date 2023-07-03 11:09:52
 */

@Slf4j
@RestController
@RequestMapping("qa/article")
public class ScdQaArticleController {

    @Resource
    ScdQaArticleService scdQaArticleService;
    /**
     * 保存
     */
    @PostMapping("/save1")
    public R save1(@RequestBody ScdQaArticleEntity scdQaCommunity) {
        log.info("********");
log.info(JSON.toJSONString(scdQaCommunity));
        return R.ok("操作成功");
    }

    /**
     * 保存
     */
    @PostMapping("/save")
     public R save(@RequestParam String content,@RequestParam  String title,
                @RequestPart(value = "file", required = false) MultipartFile file,
                @RequestParam  Long [] categoryIds
                          ) {
            log.info("********");
            log.info(JSON.toJSONString(content));
            log.info(JSON.toJSONString(categoryIds));
        String fileName = null;
        InputStream ism = null;
        try {
            if (file != null) {
                fileName = file.getOriginalFilename();
                ism = file.getInputStream();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        ScdQaArticleEntity scdQaCommunity =new ScdQaArticleEntity();
        scdQaCommunity.setTitle(title);
        scdQaCommunity.setContent(content);
        scdQaArticleService.save(scdQaCommunity);
        return R.ok("操作成功");
    }
    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody ArticlePage logPage) {
        LambdaQueryWrapper<ScdQaArticleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(logPage.getTitle()), ScdQaArticleEntity::getTitle, logPage.getTitle());
        Page<ScdQaArticleEntity> page = new Page<>(logPage.getPageNum(), logPage.getPageSize());
        IPage<ScdQaArticleEntity> iPage = this.scdQaArticleService.page(page, queryWrapper);
        return R.ok(iPage);
    }

}
